package com.bosssoft.controller;

import com.bosssoft.permission.centre.api.RoleApi;
import com.bosssoft.permission.centre.dao.mapper.ResourceMapper;
import com.bosssoft.permission.centre.entity.Node;
import com.bosssoft.permission.centre.entity.Resource;
import com.bosssoft.permission.centre.entity.User;
import com.bosssoft.permission.centre.pojo.dto.pcp.RoleDTO;
import com.bosssoft.permission.centre.pojo.dto.pcp.RoleResourceDTO;
import com.bosssoft.permission.centre.pojo.dto.pcp.UserDTO;
import com.bosssoft.permission.centre.pojo.dto.pcp.UserRoleDTO;
import com.bosssoft.permission.centre.pojo.query.RoleQuery;
import com.bosssoft.permission.centre.pojo.query.UserQuery;
import com.bosssoft.permission.centre.pojo.vo.ResourceVO;
import com.bosssoft.permission.centre.pojo.vo.RoleVO;
import com.bosssoft.permission.centre.pojo.vo.UserVO;
import com.bosssoft.permission.centre.service.RoleService;
import com.sd365.common.core.common.controller.AbstractController;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @description: 角色管理Controller实现类
 * @Author: wang
 * @Time: 2022/3/23
 */

@CrossOrigin
@RestController
public class RoleController extends AbstractController implements RoleApi {

    /**
     * 角色服务类
     */
    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceMapper resourceMapper;

    @ApiLog
    @Override
    public Boolean assignResource(@Valid RoleResourceDTO roleResourceDTO) {
        return roleService.assignResource(roleResourceDTO);
    }

    @ApiLog
    @Override
    public Boolean assignUser(@Valid UserRoleDTO userRoleDTO) {
        return roleService.assignUser(userRoleDTO);
    }

    @ApiLog
    @Override
    public List<Node> doQueryResourceByRoleId(@Valid RoleQuery roleQuery) {
        return roleService.queryResourceByRoleId(roleQuery.getId());
    }

    @Override
    public List<Node> selectCompany() {
        return roleService.selectCompany();
    }

    @ApiLog
    @Override
    public boolean haveRole(@Valid RoleDTO roleDTO) {
        return roleService.haveRole(roleDTO);
    }

    @ApiLog
    @Override
    public Boolean add(@Valid RoleDTO roleDTO) {
        return roleService.add(roleDTO);
    }

    @ApiLog
    @Override
    public Boolean remove(Long id, Long version) {
        return roleService.remove(id, version);
    }

    @ApiLog
    @Override
    public Boolean batchRemove(@Valid RoleDTO[] roleDTOS) {
        return roleService.batchRemove(roleDTOS);
    }

    @ApiLog
    @Override
    public Boolean modify(@Valid RoleDTO roleDTO) {
        if (roleService.modify(roleDTO) != null) {
            return true;
        }
        return false;
    }

    @ApiLog
    @Override
    public RoleDTO copy(Long id) {
        return roleService.copy(id);
    }

    @ApiLog
    @Override
    public List<RoleVO> commonQuery(RoleQuery roleQuery) {
        if (null == roleQuery) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        //执行查询
        List<RoleDTO> roleDTOS = roleService.commonQuery(roleQuery);
        List<RoleVO> roleVOS = BeanUtil.copyList(roleDTOS, RoleVO.class);
        if(roleVOS!=null&&roleVOS.size()>0){
            List<Resource> resources=resourceMapper.selectResourceByRoleId(roleVOS.get(0).getId());
            List<ResourceVO> resourceVOS= BeanUtil.copyList(resources,ResourceVO.class);
            for (RoleVO currentRole : roleVOS) {
                currentRole.setResourceVOS(resourceVOS);
            }
            return roleVOS;
        }else {

            return roleVOS;
        }


    }

    @ApiLog
    @Override
    public List<UserVO> commonQueryUser(UserQuery userQuery) {
        if (null == userQuery) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        //执行查询
        List<UserDTO> userDTOS = roleService.commonQueryUser(userQuery);
        List<UserVO> userVOS = BeanUtil.copyList(userDTOS, UserVO.class);

        return userVOS;
    }

    @ApiLog
    @Override
    public RoleVO queryRoleById(Long id) {
        RoleDTO roleDTO = roleService.queryById(id);
        if (roleDTO != null) {
            RoleVO roleVO = BeanUtil.copy(roleDTO, RoleVO.class);
            return roleVO;
        } else {
            return null;
        }
    }

    @Override
    public RoleVO queryUserResource(Long id) {
        RoleDTO roleDTO = roleService.queryUserResource(id);
        if (roleDTO != null) {
            RoleVO copy = BeanUtil.copy(roleDTO, RoleVO.class);
            List<ResourceVO> resourceVOS = BeanUtil.copyList(roleDTO.getResourceDTOS(), ResourceVO.class);
            copy.setResourceVOS(resourceVOS);
            return copy;
        } else return new RoleVO();
    }
}
